package com.autonavi.yunda.yunji.controller;

import com.autonavi.yunda.yunji.common.exception.AmapException;
import com.autonavi.yunda.yunji.common.exception.AmapExceptionCode;
import com.autonavi.yunda.yunji.common.exception.AmapPreconditions;
import com.autonavi.yunda.yunji.common.utils.http.HttpUtil;
import com.autonavi.yunda.yunji.core.enums.BpmsApprovalStatus;
import com.autonavi.yunda.yunji.core.enums.BpmsCallBackType;
import com.autonavi.yunda.yunji.core.model.dto.BpmsApprovalDTO;
import com.autonavi.yunda.yunji.core.model.mappers.BpmsApprovalMapper;
import com.autonavi.yunda.yunji.core.service.PartUnitService;
import com.autonavi.yunda.yunji.service.BPMSFlowService;
import com.autonavi.yunda.yunji.service.model.BpmsTaskVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * @author cizhong.wcs
 * @date 2023/7/6 15:46
 */
@RestController
@RequestMapping(value = "yunji/admin/callback")
public class CallBackController {
    @Resource
    private PartUnitService partUnitService;
    @Resource
    private BPMSFlowService bpmsFlowService;

    @Value("${yuncang.bpms.apiTestFlow.authKey}")
    private String yuncangAuthKey;

    @Resource
    private BpmsApprovalMapper bpmsApprovalMapper;

    @PostMapping(value = "bpms")
    public void bpms(HttpServletRequest request) {
        //x-www-form-urlencoded 参数解析处理
        Map<String, String> params;
        try {
            String body = IOUtils.toString(request.getInputStream(), Charset.defaultCharset());
            params = HttpUtil.getParamsMapUrlDecode(body);
        } catch (IOException e) {
            throw new AmapException(AmapExceptionCode.COMMON_ILLEGAL_PARAM);
        }
        String procInstId = params.get("procInstId");
        if (StringUtils.isNotBlank(procInstId)) {
            handleCallbackByProcInstId(procInstId);
            return;
        }
        String callBackType = params.get("callBackType");
        AmapPreconditions.checkArgumentForUser(StringUtils.isNotBlank(callBackType), "参数callBackType不能为空");
        BpmsCallBackType type = BpmsCallBackType.valueOf(callBackType);
        switch (type) {
            case UPDATE_PART_UNIT_TEST_STATUS:
                partUnitService.updateTestStatus(params);
                break;
            default:
                throw new AmapException(AmapExceptionCode.COMMON_ILLEGAL_PARAM, "不支持该类型:" + type);
        }
    }

    public void handleCallbackByProcInstId(String procInstId) {
        BpmsTaskVO bpmsTaskVO = bpmsFlowService.getByProcessInstanceId(yuncangAuthKey, procInstId);
        BpmsApprovalStatus approvalStatus = bpmsTaskVO.getBpmsApprovalStatus();
        if (BpmsApprovalStatus.INIT.equals(approvalStatus)) {
            return;
        }
        LambdaQueryWrapper<BpmsApprovalDTO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BpmsApprovalDTO::getProcInstId, procInstId);
        BpmsApprovalDTO dto = bpmsApprovalMapper.selectOne(wrapper);
        if (dto == null) {
            return;
        }
        BpmsCallBackType type = dto.getType();
        if (type.equals(BpmsCallBackType.ycOnShelfApproval)) {
            partUnitService.updateShelfStatus(dto.getRelationId(), approvalStatus);
        }
        dto.setStatus(approvalStatus);
        bpmsApprovalMapper.updateById(dto);
    }

}
